const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      tableData: [],
      rules: {
        App_ID: [{
          required: true,
          message: '请输入小程序AppID',
          trigger: 'blur',
        }],
        App_Name: [{
          required: true,
          message: '请输入小程序名称',
          trigger: 'blur',
        }],
        Store_SerialNumber: [{
          required: true,
          message: '请选择门店',
          trigger: 'change',
        }],
        App_Style_SerialNumber: [{
          required: true,
          message: '请选择样式方案',
          trigger: 'change',
        }]
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        App_ID: '',
        App_Name: '',
        App_Secret:'',
        // Row_Status: '',
        // Operation_ID: '',
        // Operation_Datetime: '',
        Children: {
          enterprise_appid_detail: [
            {
              Enterprise_Info_SerialNumber:'',
              mchid:'',
              App_Style_SerialNumber:'',
              Enterprise_Appid_Detail_SerialNumber:'',
              App_ID:'',
            }
          ],
        }
      },
      loading: false,
      list: [],
      mchidData:[],
      enterpriseData:[],
    }
  },
  mounted() {
    this.getShopName()
    this.getList();
    this.getEnterprise();
    this.getMchid();
  },
  methods: {
    // 获取门店
    getShopName() {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "store_info",
          token: $.cookie("token")
        },
        success: function (data) {
          data = data.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.tableData = data;
          console.log(_this.tableData)
        },
        dataType: "json"
      });
    },
    //查询所有企业
    getEnterprise() {
      let _this = this
      $.ajax({
        type: 'GET',
        url: DomainName + '/getEnterpriseInfoAction?token=' + $.cookie('token'),
        success: function (data) {
          _this.enterpriseData = data?.Data;
          console.log(data,'enterpriseData',_this.enterpriseData)
        },
        dataType: "json"
      });
    },
    // 查询商户号
    getMchid() {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        data: {
          Data: "wechatpay",
          isNullEnterprise:true
        },
        success: function (data) {
          _this.mchidData = data;
          console.log('mchidData',_this.mchidData)
        },
        dataType: "json"
      });
    },
    //获取样式配置方案
    getList() {
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        type: 'POST',
        data: {
          Data: "enterprise_appid_style",
        },
        success: function (res) {
          _this.list = res
          console.log(_this.list);
          console.log(res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getAppID(e){
      for(let i = 0;i<this.form.Children.enterprise_appid_detail.length;i++){
        this.form.Children.enterprise_appid_detail[i].App_ID = e
      }
      console.log(this.form);
    },
    submitForm(formName) {
      let _this = this
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(_this.form);
          _this.loading = true
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                enterprise_appid: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Result == "成功") {
                _this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                window.history.go(-1);
                // window.location.href = './Applet.html'
                // window.location.href = 'index.html';
              } else {
                _this.$message.error(res.Result);
              }
            },
            error: function (error) {
              console.log(error)
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        }
        else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 添加
    editMember() {
      this.form.Children.enterprise_appid_detail.push({
        Enterprise_Info_SerialNumber: '',
        mchid: '',
        App_Style_SerialNumber: '',
        Enterprise_Appid_Detail_SerialNumber: '',
        App_ID: this.form.App_ID,
        mchidData: [],
      });
    },
    deletMember(){
      if(this.form.Children.enterprise_appid_detail.length > 1){
        this.form.Children.enterprise_appid_detail.pop();
      }else{
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    }
  }
})